﻿using AutoMapper;
using Entity.WMS.DTO.WarehouseSale;
using Entity.WMS.View.WarehouseSale;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.WMS
{
    public class WarehouseSaleService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public WarehouseSaleService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取销售出库列表
         */
        public async Task<RequestResponseModel<PageResult<WarehouseSaleView>>> GetWarehouseSaleList(WarehouseSaleSearchDTO warehouseSaleSearchDTO)
        {
            //筛选
            var query = _context.WarehouseSaleView
                .WhereIF(a => a.CardNo.Contains(warehouseSaleSearchDTO.CardNo), !string.IsNullOrEmpty(warehouseSaleSearchDTO.CardNo))
                .WhereIF(a => a.GoodsCode.Contains(warehouseSaleSearchDTO.Goods) || a.GoodsName.Contains(warehouseSaleSearchDTO.Goods) || a.GoodsSpec.Contains(warehouseSaleSearchDTO.Goods), !string.IsNullOrEmpty(warehouseSaleSearchDTO.Goods))
                .WhereIF(a => a.WarehouseCode.Contains(warehouseSaleSearchDTO.Warehouse) || a.WarehouseName.Contains(warehouseSaleSearchDTO.Warehouse), !string.IsNullOrEmpty(warehouseSaleSearchDTO.Warehouse))
                .WhereIF(a => a.WarehousePositionCode.Contains(warehouseSaleSearchDTO.WarehousePosition) || a.WarehousePositionName.Contains(warehouseSaleSearchDTO.WarehousePosition), !string.IsNullOrEmpty(warehouseSaleSearchDTO.WarehousePosition))
                ;

            //返回列表
            var warehouseSale = await query
                .OrderBy(a => a.CreateTime)
                .Page(warehouseSaleSearchDTO.CurrentPage, warehouseSaleSearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<WarehouseSaleView>(warehouseSale, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }
    }
}
